New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add --from-post=<post_id>
flag to create duplicate posts
#154
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Great start! Can you include functional tests too?
src/Post_Command.php
Outdated
* | ||
* [--from-post=<post_id>] | ||
* : The post id of a post to be duplicated. | ||
* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this is spaces instead of tabs. Can you correct to tabs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, I will correct it.
src/Post_Command.php
Outdated
* # Create a duplicate post with same post data. | ||
* $ wp post create --from-post=123 --post_title='Different Title' | ||
* Success: Created post 2350. | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto spaces -> tabs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad. I will change it too.
src/Post_Command.php
Outdated
$post_id = $post_arr['ID']; | ||
unset( $post_arr['post_date'] ); | ||
unset( $post_arr['post_date_gmt'] ); | ||
unset( $post_arr['guid'] ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WHy did you decide to skip these values?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This values will be duplicated too. I skip post_date and post_date_gmt so that values can be reset according to the current date at the time of duplicating. Reason to unset guid is that duplicated post guid will same as the original.
If you have any suggestions please let me know.
src/Post_Command.php
Outdated
if( isset( $assoc_args['from-post'] ) ) { | ||
$post = $this->fetcher->get_check( $assoc_args['from-post'] ); | ||
$post_arr = get_object_vars( $post ); | ||
$post_id = $post_arr['ID']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$post_arr['ID']
should be unset, correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, you are right. Actually, if we don't unset id it will still generate new id. But, for a safer side, I will make correct it.
src/Post_Command.php
Outdated
* @param $post_id id of the post. | ||
* @return array | ||
*/ | ||
private function get_metadata( $post_id = null ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of making $post_id
optional, we should make it required:
get_metadata( $post_id = null )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure.
src/Post_Command.php
Outdated
|
||
if ( empty( $assoc_args['meta_input'] ) ) { | ||
$assoc_args['meta_input'] = $this->get_metadata( $post_id ); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to persist taxonomy data too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part is for overwriting existing metadata if a user wants. Categories and tags can be duplicated too. what are your thoughts about it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think categories and tags should be duplicated too.
@danielbachhuber I made changes you asked and added taxonomy duplication as well. I don't have any idea about functional testing so I haven't done testing yet. |
@sagarnasit I can help you go through setting up the testing. You should first check whether you can even run the existing tests. As the Functional Tests help states, you'll need to prepare your database server first:
|
Looking into the Travis failure: #161 |
@sagarnasit I should have addressed the test failure on PHP 5.3 now. Can you merge latest master into your PR to make the tests pass again? |
Thanks @schlessera . I also added functional testing as @danielbachhuber said. |
""" | ||
{TERM_ID} | ||
""" | ||
@require-wp-4.4 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Blank line needed between scenarios (above the @require-wp-4.4
tag).
src/Post_Command.php
Outdated
@@ -171,7 +178,28 @@ public function create( $args, $assoc_args ) { | |||
} | |||
|
|||
$array_arguments = array( 'meta_input' ); | |||
$assoc_args = \WP_CLI\Utils\parse_shell_arrays( $assoc_args, $array_arguments ); | |||
$assoc_args = \WP_CLI\Utils\parse_shell_arrays($assoc_args, $array_arguments); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This entire block (lines 181-202) seems to have been inadvertently reformatted to PSR2 with the last commit.
Please adapt the formatting to adhere to WPCS instead.
src/Post_Command.php
Outdated
* | ||
* @return array | ||
*/ | ||
private function get_tags( $post_id ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code path for tags is not covered by the tests.
@sagarnasit This looks good already. There's some whitespace that got messed up during the last commit that you need to redress, and I would like to also have a test that covers the "tags" code. |
@schlessera Added Test for Tags. Looks Good? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code looks good. Only some minor stuff as a final cleanup, and then we can merge.
src/Post_Command.php
Outdated
/** | ||
* Get Tags of a post. | ||
* | ||
* @param $post_id postid of the post. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be:
@param $post_id ID of the post.
src/Post_Command.php
Outdated
/** | ||
* Get Categories of a post. | ||
* | ||
* @param $post_id postid of the post. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be:
@param $post_id ID of the post.
src/Post_Command.php
Outdated
/** | ||
* Get post metadata. | ||
* | ||
* @param $post_id id of the post. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be:
@param $post_id ID of the post.
|
||
When I run `wp post term add {POST_ID} post_tag {TAG_ID} --by=id` | ||
Then STDOUT should contain: | ||
"""* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a random asterisk added here.
@schlessera Made changes you asked for. |
--from-post<post_id>
flag to create duplicate posts
--from-post<post_id>
flag to create duplicate posts--from-post=<post_id>
flag to create duplicate posts
Thank you for the pull request, @sagarnasit ! |
I here modified the create command to generate a duplicate post.